/*
 * Copyright (c) 2012 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <memory.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <sys/socket.h>

#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <signal.h>

#include <termios.h>
#include <netinet/in.h>





int main(int argc, char **argv)
{
	int valuefd, exportfd, directionfd;
	unsigned int gpio_index;
	unsigned int value;
	char gpio_index_str[16];
	char gpio_val_str[16];
	char gpio_direction_str[1024];
	char gpio_value_str[1024];

	if(argc < 2)
	{
		printf("Input error first parameter is gpio pin index,second parameter is gpio value\n");
		printf("example ./gpio_sdi 961 1\n");
		return 0;
	}

	gpio_index=atoi(argv[1]);
	value=atoi(argv[2]);

	if(gpio_index<960||gpio_index>976)
	{
		printf("Error GPIO PIN index from 960-976\n");
		return 0;
	}
	// The GPIO has to be exported to be able to see it
	// in sysfs
	exportfd = open("/sys/class/gpio/export", O_WRONLY);
	if (exportfd < 0)
	{
		printf("Error Cannot open GPIO to export it\n");
		return 0;
	}

	sprintf(gpio_index_str,"%d",gpio_index);

//	printf("gpio_index_str is %s\n",gpio_index_str);

	sprintf(gpio_val_str,"%d",value);

//	printf("gpio_val_str is %s\n",gpio_val_str);

	sprintf(gpio_direction_str,"/sys/class/gpio/gpio%d/direction",gpio_index);

//	printf("gpio_direction_str is %s\n",gpio_direction_str);

	sprintf(gpio_value_str,"/sys/class/gpio/gpio%d/value",gpio_index);

//	printf("gpio_value_str is %s\n",gpio_value_str);

#if 1
//	write(exportfd, "240", 4);
	write(exportfd, gpio_index_str, 4);
	close(exportfd);
//	printf("GPIO exported successfully\n");



	// Update the direction of the GPIO to be an output
//	directionfd = open("/sys/class/gpio/gpio240/direction", O_RDWR);

	directionfd = open(gpio_direction_str, O_RDWR);
	if (directionfd < 0)
	{
		printf("Cannot open GPIO direction it\n");
		return 0;
	}
	write(directionfd, "out", 4);
	close(directionfd);
//	printf("GPIO direction set as output successfully\n");
	// Get the GPIO value ready to be toggled

	valuefd = open(gpio_value_str, O_RDWR);
	if (valuefd < 0)
	{
		printf("Cannot open GPIO value\n");
		return 0;
	}

//	printf("GPIO value opened, now toggling...\n");
	// toggle the GPIO as fast a possible forever, a control c is needed
	// to stop it

	write(valuefd,gpio_val_str, 2);
	printf("GPIO value setting ok\n");

#endif
	return 0;
}
